﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PocketPond.PondConsumer;
using PocketPond.Core;
using PocketPond.Core.Types;
using System.Net;
using System.IO;
using PondConsumer;
using LightControls;
using System.Reflection;

namespace PocketPond
{
    public partial class PocketPond : Form
    {
		
        public PocketPond()
        {
            InitializeComponent();
			refreshTimeline();
        }

		public enum Timeline
		{
			To_Me = 1,
			General
		}
		public Timeline timeline_status = Timeline.General;

		void GeneralTimeline_Click(object sender, EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;
			timeline_status = Timeline.General;

            timeline.RemoveAll();
            Event[] arrEvents = PondRequest.getEventsList(ClientSettings.AuthToken);
            foreach (Event ev in arrEvents)
            {
                if (!File.Exists(ev.AvatarFile))
                {
                    if (!Avatar.downloadAvatar(ev.AvatarUrl, ev.AvatarFile))
                    {
                        ev.AvatarFile = "default.jpg";
                    }
                }

                Bitmap bmp = new Bitmap(ev.AvatarFile);
                SliderItem item = new SliderItem(ev.AuthorScreenName,
                                                 ev.ServiceId.ToString(),
                                                 ev.TruncatedData,
                                                 ev.PublishedAt.ToString(),
                                                 bmp,
                                                 ev);
                timeline.AddItem(item);
            }
            Cursor.Current = Cursors.Default;
		}

		void To_Me_Click( object sender, EventArgs e )
		{
			Cursor.Current = Cursors.WaitCursor;
	
			timeline_status = Timeline.To_Me;

            timeline.RemoveAll();

            Event[] arrEvents = PondRequest.getEventsToMe(ClientSettings.AuthToken);
            foreach (Event ev in arrEvents)
            {
                if (!File.Exists(ev.AvatarFile))
                {
                    if (!Avatar.downloadAvatar(ev.AvatarUrl, ev.AvatarFile))
                    {
                        ev.AvatarFile = "default.jpg";
                    }
                }

                Bitmap bmp = new Bitmap(ev.AvatarFile);
                SliderItem item = new SliderItem(ev.AuthorScreenName,
                                                 ev.ServiceId.ToString(),
                                                 ev.TruncatedData,
                                                 ev.PublishedAt.ToString(),
                                                 bmp,
                                                 ev);
                timeline.AddItem(item);
            }
            Cursor.Current = Cursors.Default;
		}

        void Reply_Click(object sender, EventArgs e)
        {
            if ( timeline.SelectedItem != null )
            {
                SliderItem item = (SliderItem)timeline.SelectedItem;
                Core.Event ev = (Core.Event)item.Value;

                PostEvent dlg = new PostEvent(ev);
                dlg.ShowDialog();
            }
        }

		void ViewDetails_Click( object sender, EventArgs e )
		{
            if (timeline.SelectedItem != null)
            {
                SliderItem item = (SliderItem)timeline.SelectedItem;
                Core.Event ev = (Core.Event)item.Value;
				String eventID = ev.EventId;

				//Core.Event eventDetail = PondRequest.getEventDetails(ClientSettings.AuthToken, ev.EventId);
				if (!File.Exists(ev.AvatarFile))
                {
                    if (!Avatar.downloadAvatar(ev.AvatarUrl, ev.AvatarFile))
                    {
                        ev.AvatarFile = "default.jpg";
                    }
                }

                Bitmap bmp = new Bitmap(ev.AvatarFile);
                detailsControl1.LoadInfo(ev.AuthorName, ev.ServiceId.ToString(), ev.TruncatedData, ev.PublishedAt.ToString(), bmp);
				detailsControl1.Visible = true;
			}
		}

        void Settings_Click(object sender, EventArgs e)
        {
            Settings dlg = new Settings();
            dlg.ShowDialog();
            refreshTimeline();
        }

        void Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void Refresh_Click(object sender, EventArgs e)
        {
            refreshTimeline();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            refreshTimeline();
        }

        private void refreshTimeline()
        {
            Cursor.Current = Cursors.WaitCursor;
            Event[] arrEvents;

            timeline.RemoveAll();
			if ( timeline_status == Timeline.General)
			{
				arrEvents = PondRequest.getEventsList( ClientSettings.AuthToken, ClientSettings.OnlyUnread );
			}
			else 
			{
                arrEvents = PondRequest.getEventsToMe(ClientSettings.AuthToken, ClientSettings.OnlyUnread);
			}
			
            foreach (Event ev in arrEvents)
            {
                // Check avatar
                if (!File.Exists(ev.AvatarFile))
                {
                    if (!Avatar.downloadAvatar(ev.AvatarUrl, ev.AvatarFile))
                    {
                        ev.AvatarFile = Core.Utils.getPocketPondDir() + "\\avatars\\default.jpg";
                    }
                }

                Bitmap bmp = new Bitmap(ev.AvatarFile);
                SliderItem item = new SliderItem(ev.AuthorName, 
                                                 ev.ServiceId.ToString(), 
                                                 ev.TruncatedData, 
                                                 ev.PublishedAt.ToString(), 
                                                 bmp,
                                                 ev);
                timeline.AddItem(item);
            }
            Cursor.Current = Cursors.Default;
        }

        private void PostUpdate_Click(object sender, EventArgs e)
        {
            PostEvent dlg = new PostEvent();
            dlg.ShowDialog();
        }

        private static Dictionary<Services, Account> _pondAccounts = null;

        public static Dictionary<Services,Account> Accounts
        {
            get
            {
                if (_pondAccounts == null)
                {
                    _pondAccounts = new Dictionary<Services,Account>();
                    Account[] arrAccounts = PondRequest.getAccountList(ClientSettings.AuthToken);
                    foreach (Account acc in arrAccounts)
                    {
                        _pondAccounts.Add(acc.ServiceId, acc);
                    }
                }

                return _pondAccounts;
            }
        }

        private void menuItem11_Click(object sender, EventArgs e)
        {
            List<String> lstEventIds = new List<string>();
            foreach (SliderItem item in timeline.Items)
            {
                lstEventIds.Add(((Event)item.Value).EventId);
            }
            PondRequest.setEventRead(ClientSettings.AuthToken, true, lstEventIds.ToArray());

            if (ClientSettings.OnlyUnread)
            {
                refreshTimeline();
            }
        }
    }
}